<?php
namespace App\Repositories\BLL;
use App\Repositories\BLL\Repository;
use App\Models\Admins\Menu;
use Illuminate\Support\Facades\DB;
use Cache;
class MenuBLL extends Repository
{
	public function model()
	{
		return Menu::class;
	}
	
	/**
	 * 写入菜单及权限
	 */
	public function create(array $attributes){
		//获取层级
		if($attributes['pid'] == 0){
			$attributes['level'] = 1;
		}else{
			$pinfo = $this->find($attributes['pid']);
			$attributes['level'] = $pinfo['level']+1;
		}
		$time = date("Y-m-d H:i:s");
		//事物新增
		DB::beginTransaction();
		try{
			//写入菜单 
			DB::table('menus')->insert(['name'=>$attributes['name']
												,'pid'=>$attributes['pid']
												,'is_url'=>$attributes['is_url']
												,'icon'=>$attributes['icon']
												,'remark'=>$attributes['remark']
												,'sort'=>$attributes['sort']
												,'permission'=>$attributes['permission']
												,'is_show'=>$attributes['is_show']
												,'level'=>$attributes['level']
												,'created_at'=>$time
												,'updated_at'=>$time
												]);
			//检查权限是否存在
			$permission = DB::table('permissions')->where('name', $attributes['permission'])->first();
			if(empty($permission)){
				//写入权限
		    		DB::table('permissions')->insert(['name'=>$attributes['permission']
		    												,'display_name'=>$attributes['name']
		    												,'description'=>$attributes['remark']
		    												,'created_at'=>$time
														,'updated_at'=>$time
		    												]);
			}
			
			DB::commit(); 
			return TRUE;
		}catch (\Exception $e) { 
			DB::rollBack(); 
			return FALSE;
		}
	}
	
	public function getMenuList()
	{
		// 判断数据是否缓存
		if (Cache::has(config('admins.globals.cache.menulist'))) {
//			dd(Cache::get(config('admins.globals.cache.menulist')));
			return Cache::get(config('admins.globals.cache.menulist'));
		}
		return $this->getMenuListCache();
	}
	
	/**
	 * 获取菜单列表并缓存
	 */
	public function getMenuListCache(){
		$menus = $this->model->orderBy('pid')->orderBy('sort','desc')->get()->toArray();
		if($menus){
			$newMenus = $this->setMenuArray($menus);
			
			// 缓存菜单数据
			Cache::forever(config('admins.globals.cache.menulist'),$newMenus);
			return $newMenus;
		}
		return "";
	}
	
	/**
	 * 整理菜单数组结构
	 */
	private function setMenuArray($menus , $pid = 0){
		if (empty($menus)) {
			return '';
		}
		$newMenus = [];
		foreach($menus as $key => $v){
			if($v['pid'] == $pid){
				$newMenus[$key] = $v;
				$newMenus[$key]['child'] = self::setMenuArray($menus,$v['id']);
			}
		}
		
		return $newMenus;
	}
	/**
	 * 获取菜单详情
	 */
	public function editMenu($id)
	{
		$menu = $this->model->find($id)->toArray();
		if ($menu) {
			$menu['update'] = url('admin/menu/'.$id);
    		$menu['msg'] = '加载成功';
    		$menu['status'] = true;
			return $menu;
		}
		return ['status' => false,'msg' => '加载失败'];
	}
	/**
	 * 更新菜单
	 */
	public function updateMenu($request)
    {
        $menu = $this->model->find($request->id);
		if ($menu) {
			$isUpdate = $menu->update($request->all());
			if ($isUpdate) {
				$this->getMenuListCache();
				flash('修改菜单成功', 'success');
				return true;
			}
			flash('修改菜单失败', 'danger');
			return false;
		}
		abort(404,'菜单数据找不到');
    }
	
	/**
	 * 删除
	 */
	public function destroyMenu($id){
	    	$isDelete = $this->model->destroy($id);
	    	if ($isDelete) {
	    		// 更新缓存数据
	    		$this->getMenuListCache();
	    		flash('删除菜单成功', 'success');
				return true;
	    	}
	    	flash('删除菜单失败', 'error');
		return false;
    }
}